package wang.hongyun.controller;

import com.alibaba.fastjson.JSON;
import com.dingtalk.common.Env;
import com.dingtalk.common.auth.AuthHelper;
import com.dingtalk.open.client.ServiceFactory;
import com.dingtalk.open.client.api.model.corp.CorpUserBaseInfo;
import com.dingtalk.open.client.api.model.corp.CorpUserDetail;
import com.dingtalk.open.client.api.service.corp.CorpUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import wang.hongyun.common.ServerResponse;
import wang.hongyun.service.IUserService;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018-07-06.
 * 根据免登授权码查询免登用户信息
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 根据免登授权码查询免登用户userId
     *
     * @param code
     * @param corpid
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getUserInfo.do")
    @ResponseBody
    public  ServerResponse<String> getUserInfo(String code, String corpid) throws Exception {
        if (corpid != null && corpid!="" && Env.CORP_ID.equals(corpid)){
            String accToken = AuthHelper.getAccessToken();
            String userid = iUserService.getUserId(accToken,code).toString();
            String user = iUserService.getUser(accToken,userid);
            return ServerResponse.createBySuccess("获取成功",user);
        }else{
            return  ServerResponse.createByErrorMessage("corpid错误");
        }
    }

}
